package com.HQCourse.activity.activity.service.impl;


import com.HQCourse.activity.activity.service.CouponUseService;


import com.HQCourse.clilent.userchilent.UserInfoFeignClient;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.HQCourse.model.activity.CouponInfo;
import com.HQCourse.activity.activity.mapper.CouponInfoMapper;
import com.HQCourse.activity.activity.service.CouponInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.HQCourse.model.activity.CouponUse;
import com.example.HQCourse.model.user.UserInfo;
import com.example.HQCourse.vo.activity.CouponUseQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 优惠券信息 服务实现类
 * </p>
 *优惠卷接口
 * @author zww
 * @since 2023-07-03
 */
@Service
public class CouponInfoServiceImpl extends ServiceImpl<CouponInfoMapper, CouponInfo> implements CouponInfoService {
   @Autowired
    private CouponUseService couponUseService;
   @Autowired
   private UserInfoFeignClient userInfoFeignClient;
   @Override
    public IPage<CouponUse> selectCouponUsePage(Page<CouponUse> pageParam, CouponUseQueryVo couponUseQueryVo) {
       Long couponId = couponUseQueryVo.getCouponId();
       String couponStatus = couponUseQueryVo.getCouponStatus();
       String getTimeBegin = couponUseQueryVo.getGetTimeBegin();
       String getTimeEnd = couponUseQueryVo.getGetTimeEnd();
       QueryWrapper<CouponUse> wrapper=new QueryWrapper<>();
       if(!StringUtils.isEmpty(couponId)) wrapper.eq("coupon_id",couponId);
       if(!StringUtils.isEmpty(couponStatus)) wrapper.eq("coupon_status",couponId);
       if(!StringUtils.isEmpty(couponId)) wrapper.ge("get_time",getTimeBegin);
       if(!StringUtils.isEmpty(couponId)) wrapper.le("coupon_time",getTimeEnd);
       IPage<CouponUse> page = couponUseService.page(pageParam, wrapper);
       List<CouponUse> records = page.getRecords();
       records.stream().forEach(a->this.getUserInfoById(a));
       return page;
    }
    private CouponUse getUserInfoById(CouponUse a) {
        Long userId = a.getUserId();
        if(!StringUtils.isEmpty(userId)){
            UserInfo userInfo = userInfoFeignClient.getById(userId);
            if(userInfo!=null){
                a.getParam().put("nickName",userInfo.getNickName());
                a.getParam().put("phone",userInfo.getPhone());
            }
        }
          return a;
    }

    @Override
    public void updateCouponInfoUseStatus(Long couponUseId, Long orderId) {
        CouponUse couponUse = new CouponUse();
        couponUse.setId(couponUseId);
        couponUse.setOrderId(orderId);
        couponUse.setCouponStatus("1");
        couponUse.setUsingTime(new Date());
        couponUseService.updateById(couponUse);

    }
}
